package com.wlz.config;

import com.wlz.bean.wlz;
import com.wlz.bean.User;
import com.wlz.core.MyImportBeanDefinitionRegistrar;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Profile;
import org.springframework.core.io.ClassPathResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;

/**
 * wlz
 *
 * http://bat.ke.qq.com
 * 往期视频加群:516212256 暗号:6
 */
@Configuration
@MapperScan("com.wlz.mapper")
@Import(MyImportBeanDefinitionRegistrar.class)
// beanDefinition cglib    full/lite
public class AppConfig {

    @Bean
    public User user(){
        return new User();
    }

    @Bean

    public wlz wlz(){
        return new wlz();
    }

    @Bean
    @Profile("dev")
    public PropertyPlaceholderConfigurer devPropertyPlaceholderConfigurer() {
        PropertyPlaceholderConfigurer propertyPlaceholderConfigurer=new PropertyPlaceholderConfigurer();
        propertyPlaceholderConfigurer.setLocation(new ClassPathResource("application-dev.properties"));
        return propertyPlaceholderConfigurer;
    }

    @Bean
    @Profile("prod")
    public PropertyPlaceholderConfigurer prodPropertyPlaceholderConfigurer() {
        PropertyPlaceholderConfigurer propertyPlaceholderConfigurer=new PropertyPlaceholderConfigurer();
        propertyPlaceholderConfigurer.setLocation(new ClassPathResource("application-prod.properties"));
        return propertyPlaceholderConfigurer;
    }


    @Bean
    public DataSource dataSource(){
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test?characterEncoding=utf8");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        return dataSource;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        factoryBean.setDataSource(dataSource());
        return factoryBean.getObject();
    }


}
